const mongoose = require('mongoose');

const connection = 'mongodb+srv://2818975440:19931573138%2E%40asd@cluster0.bjslyrs.mongodb.net';

mongoose.connect(connection, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    dbName: 'order_management' // 指定数据库名称
})
    .then(() => {
        console.log('MongoDB 连接成功！');
    })
    .catch((err) => {
        console.error('MongoDB 连接失败:', err);
    });



// 李键良
const registerSchema = new mongoose.Schema({
    username: String,
    password: String
})

// 商品模型
const FoodSchema = new mongoose.Schema({
    name: String,
    price: Number,
    num: {
        type: Number,
        default: 0
    },
    cid: {
        type: mongoose.Types.ObjectId,
        ref: "Food"
    },
    img: String
});

// 订单模型
const OrderSchema = new mongoose.Schema({
    userId: {
        type: mongoose.Types.ObjectId,
        required: true
    },
    items: [{
        productId: {
            type: mongoose.Types.ObjectId,
            ref: 'Food',
            required: true
        },
        title: {
            type: String,
            required: true
        },
        price: {
            type: Number,
            required: true
        },
        quantity: {
            type: Number,
            default: 1
        }
    }],
    totalAmount: {
        type: Number,
        required: true
    },
    status: {
        type: String,
        enum: ['unpaid', 'unsent', 'unreceived', 'uncommented', 'completed'],
        default: 'unpaid'
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    },
    shippingAddress: String,
    contactPhone: String
});

// 会员表
const MemberSchema = new mongoose.Schema({
    memberType: {
        type: String,//会员类型
        enum: ['普通会员', '黄金会员'],
        default: '普通会员'
    },
    startDate: {
        //会员开始日期
        type: Date,
        default: Date.now
    },
    expireDate: {
        //会员结束日期
        type: Date,
        required: true
    },
    amount:Number,//购买金额
    isAutoRenew: {
        //是否自动续费
        type: Boolean,
        default: false
    },
    vipLevel: {
        //VIP等级
        type: Number,
        default: 1,
        min: 1,
        max: 5
    }
});

// 添加虚拟字段用于格式化时间
OrderSchema.virtual('formattedTime').get(function () {
    return this.createdAt.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
    }).replace(/\//g, '-');
});

// 在转换为JSON时包含虚拟字段
OrderSchema.set('toJSON', { virtuals: true });

const FoodModel = mongoose.model('Food', FoodSchema, "Food");
const OrderModel = mongoose.model('Order', OrderSchema, "Orders");

const registerModel = mongoose.model("register", registerSchema, "register");

const MemberModel = mongoose.model('Member', MemberSchema, "Member");



mongoose.connection.on('error', console.error);

module.exports = {
    FoodModel,
    OrderModel,

    registerModel,

    MemberModel,

};